
var tools = function () {
    var TickS = []
    createjs.addTick = (f) => {
        TickS.push(f)
    }
    createjs.Ticker.addEventListener("tick", handleTick);
    function handleTick(event) {
        // Actions carried out each tick (aka frame)
        if (!event.paused) {
            // 无限循环的函数
            TickS.forEach((f) => {
                f()
            })
        }
    }

    //***********************
    //只执行一次方法
    function once(fn) {
        let caller = true;
        // console.log(caller)
        return function () {
            if (caller) {
                caller = false
                // console.log(caller)
                fn.apply(this, arguments)
            }
        }
    }

    //双击或者多击事件
    window.G_doubleClick = (handle, awt = 500, n = 2) => {
        let count = 0;
        let timer = null;
        return function () {
            if (!timer) {
                timer = setTimeout(function () {
                    count = 0;
                    timer = null;
                }, awt);
            }
            count++;
            if (count === n) {
                handle();
                count = 0;
                clearTimeout(timer);
                timer = null;
            }
        }
    }
    //延迟执行函数
    // function G_Delay(func, time) {
    //     setTimeout(function () {
    //         func()
    //     }, time * 1000)
    // }
    //1拖动对象2.是否画线
    window.G_drag = function (...agr) {
        agr.forEach(function (e) {
            //检测显示对象是否与鼠标点碰撞
            e.on("mousedown", function () {
                e.drag_bool = true
                if (e.mousedown != undefined) {
                    e.mousedown(e)
                }
                //深度设置
                G_setIndex(e)
            })
            //鼠标抬起
            e.on("pressup", function () {
                e.drag_bool = false
                if (e.mouseup != undefined) {
                    // console.log(this.obj.stagemouseup)
                    e.mouseup()
                }
            })
            //鼠标移动
            e.on("pressmove", function (a) {
                if (!e.drag_bool) { return }
                var pt = e.parent.globalToLocal(a.stageX, a.stageY)
                e.x = pt.x
                e.y = pt.y
                if (e.mousemove != undefined) {
                    e.mousemove(e)
                }
            })
        });
    }

    //1拖动对象，限制x轴或者y轴
    window.G_drag2 = function (obj, xy, section) {
        //检测显示对象是否与鼠标点碰撞
        obj.section = section//区间，运动的范围[min,max]
        obj.xy = xy//是x轴运动还是y轴
        obj.on("mousedown", function () {
            obj.drag_bool = true
            if (obj.mousedown != undefined) {
                obj.mousedown()
            }
            //深度设置
            G_setIndex(obj)
        })
        //鼠标抬起
        obj.on("pressup", function () {
            obj.drag_bool = false
            if (obj.mouseup != undefined) {
                obj.mouseup()
            }
        })
        //鼠标移动
        obj.on("pressmove", function (a) {
            if (!obj.drag_bool) { return }
            let pt = obj.parent.globalToLocal(a.stageX, a.stageY)
            if (obj.xy == "x" && obj.section[0] <= pt.x && obj.section[1] >= pt.x) {
                obj.x = pt.x
            } if (obj.xy == "y" && obj.section[0] <= pt.y && obj.section[1] >= pt.y) {
                obj.y = pt.y
            } else if (obj.xy == undefined) {
                obj.x = pt.x
                obj.y = pt.y
            }
            if (obj.mousemove != undefined) {
                obj.mousemove()
            }
        })
    }

    //深度设置，1,个参数的时候，设置最高，2个参数的时候交换
    window.G_setIndex = (obj1, obj2) => {
        let op1 = obj1.parent
        if (obj2 == undefined) {
            op1.swapChildrenAt(op1.getChildIndex(obj1), op1.numChildren - 1)
        } else {
            if (obj1.parent.id != obj2.parent.id) {
                throw console.error("不是同一个父级，不能交换深度");
            }
            op1.swapChildren(obj1, obj2)
        }
    }

    ///;;........................................................
    //与多个点检测//如果发生了碰撞返回碰撞的对象，没有碰撞就返回false
    window.G_hit2 = function (target, ptArr) {
        for (let i = 0; i < ptArr.length; i++) {
            if (G_hit(target, ptArr[i]) != false) {
                //  console.log("发生了碰撞=" + ptArr[i].name)
                //发生了碰撞
                return target
            }
        }
        return false
    }
    //碰撞检查//如果发生了碰撞返回碰撞的对象，没有碰撞就返回false
    window.G_hit = function (target, pt) {
        // console.log(pt.parent)
        if (target == undefined || pt == undefined || pt.parent == null) {
            return
        }
        pt = pt.parent.localToLocal(pt.x, pt.y, target)
        //pt = target.globalToLocal(pt.x, pt.y)
        if (target.hitTest(pt.x, pt.y) && target != undefined) {
            return target
        } else {
            return false
        }
    }



    ///;;........................................................
    //重力函数
    var G = 0.5
    //var G_startSpeed = 0
    const G_objs = new Map()
    //console.log(G_objs.values())
    window.G_add_gravity = function (obj, callBack) {
        obj.G_startSpeed = 0
        G_objs.set(obj.id, obj)
        obj.gravity_callback_f = callBack
    }
    window.G_remove_gravity = function (obj) {
        if (G_objs.has(obj.id)) {
            G_objs.delete(obj.id)
        }
    }
    createjs.addTick(() => {
        G_objs.forEach(function (v) {
            v.G_startSpeed += G
            v.y += v.G_startSpeed
            if (v.gravity_callback_f != undefined) {
                v.gravity_callback_f()
            }
            // console.log(v.G_startSpeed)
        })
    })


    //--------------------------------------------------------------------------------------
    //适量转位图
    window.G_SSB = function (obj, f, bool) {
        let pt = new createjs.Point(obj.x, obj.y)
        let parent = obj.parent
        if (!bool) {
            parent.removeChild(obj)
        }
        //var huo = new lib.huo(); //矢量影片剪辑
        var spritesheetBuilder = new createjs.SpriteSheetBuilder();
        spritesheetBuilder.addMovieClip(obj);
        spritesheetBuilder.addEventListener("progress", function (event) {
            //console.log(event, "<<<<<<<<<<<<<<<<") //渲染进度
            //alert(6)
        })
        spritesheetBuilder.addEventListener("complete", function (event) {
            //console.log("complete", spritesheetBuilder.spriteSheet)
            spritesheetBuilder.stopAsync();
            renderCompleteHandler(spritesheetBuilder.spriteSheet)
        })
        spritesheetBuilder.buildAsync();
        function renderCompleteHandler(sheet) {
            //spritesheet = sheet;
            let sprite = new createjs.Sprite(sheet);
            sprite.x = pt.x
            sprite.y = pt.y
            parent.addChild(sprite); //在这里矢量已经变成位图
            // console.log(sprite)
            f(sprite)
            //sprite.play()
        }
    }


    //适量转位图
    window.G_SSB2 = function (obj, parent, x, y, f) {
        //var huo = new lib.huo(); //矢量影片剪辑
        var spritesheetBuilder = new createjs.SpriteSheetBuilder();
        spritesheetBuilder.addMovieClip(obj);
        spritesheetBuilder.addEventListener("progress", function (event) {
            //console.log(event, "<<<<<<<<<<<<<<<<") //渲染进度
            //alert(6)
        })
        spritesheetBuilder.addEventListener("complete", function (event) {
            //console.log("complete", spritesheetBuilder.spriteSheet)
            spritesheetBuilder.stopAsync();
            renderCompleteHandler(spritesheetBuilder.spriteSheet)
        })
        spritesheetBuilder.buildAsync();
        function renderCompleteHandler(sheet) {
            //spritesheet = sheet;
            var sprite = new createjs.Sprite(sheet);
            parent.addChild(sprite); //在这里矢量已经变成位图
            sprite.x = x
            sprite.y = y
            f(sprite)
            //sprite.play()
        }
    }

    //适量转位图
    window.G_SSB2 = function (obj, f) {
        //var huo = new lib.huo(); //矢量影片剪辑
        var spritesheetBuilder = new createjs.SpriteSheetBuilder();
        spritesheetBuilder.addMovieClip(obj);
        //spritesheetBuilder.addMovieClip(obj2);
        //spritesheetBuilder.addFrame(obj2)
        spritesheetBuilder.addEventListener("progress", function (event) {
            //console.log(event, "<<<<<<<<<<<<<<<<") //渲染进度
            //alert(6)
        })
        spritesheetBuilder.addEventListener("complete", function (event) {
            //console.log("complete", spritesheetBuilder.spriteSheet)
            spritesheetBuilder.stopAsync();
            renderCompleteHandler(spritesheetBuilder.spriteSheet)
        })
        spritesheetBuilder.buildAsync();
        function renderCompleteHandler(sheet) {
            //spritesheet = sheet;
            f(sheet)
            //sprite.play()
        }
    }
}()